import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-30
 * Time: 22:05
 */
public class MyBinaryTree {

    // 前序遍历
    void preOrder(TreeNode root){
        //回退条件
        if (root == null) {
            return;
        }

        //打印这个根结点的值
        System.out.print(root.val + " ");

        //打印这个根结点的左子树
        preOrder(root.left);

        //打印这个根结点的右子树
        preOrder(root.right);
    }

    // 前序遍历_非递归实现
    void preOrderNor(TreeNode root) {
        //定义栈和cur
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        //进入外部循环，用于遍历整棵树
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                System.out.print(cur.val + " ");
                stack.push(cur);
                cur = cur.left;
            }
            /*
            当走出循环时，说明根结点的左子树的每个结点的左子树都遍历完了
            现在要开始遍历右子树了
            */
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 中序遍历_非递归实现
    void inOrderNor(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            //注意，这里打印的是弹出栈顶结点的值！走出上面的循环时，cur == null
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 后序遍历_非递归实现
    void postOrderNor(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        //prev 用于记录上一个被访问的结点
        TreeNode prev = null;

        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //这里不能直接弹出栈顶结点，需要先获取判断一下
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev) {
                System.out.print(top.val + " ");
                stack.pop();
                //记录上一个被访问结点
                prev = top;
            }else {
                cur = top.right;
            }
        }
    }

    //构造一棵二叉树
    public TreeNode func() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');

        A.left = B;
        A.right = C;
        B.left = D;
        C.left = E;
        C.right = F;

        return A;
    }

    // 获取树中节点的个数
    private int TreeNodeSize;
    public int size(TreeNode root) {
        countTreeNodeSize(root);
        return TreeNodeSize;
    }
    //通过前序遍历统计结点个数
    private void countTreeNodeSize(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNodeSize++;
        countTreeNodeSize(root.left);
        countTreeNodeSize(root.right);
    }

    // 获取树中结点的个数_子问题思路
    public int size1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        // 当前结点 + 当前结点的左子树结点数 + 当前结点的右子树结点数
        return 1 + size1(root.left) + size1(root.right);
    }

    // 获取叶子结点的个数
    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        //判断是否是叶子结点
        if (root.left == null && root.right == null) {
            return 1;
        }
        //返回该结点左子树的叶子结点个数与右子树的叶子结点个数之和
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    // 获取第K层结点的个数
    public int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        //该结点位于第一层，返回1
        if (k == 1) {
            return 1;
        }
        //返回这个结点的左子树的第K-1层结点数与右子树的第K-1层结点数之和
        return getKLevelNodeCount(root.left,k - 1)
                + getKLevelNodeCount(root.right,k - 1);
    }

    // 获取二叉树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        //分别获取该结点左子树和右子树的高度
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        //通过三目操作符取得该结点左子树和右子树高度的最大值，再+1返回
        return (leftHeight > rightHeight?leftHeight:rightHeight) + 1;
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        //如果当前结点就是要查找的结点，直接返回
        if (root.val == val) {
            return root;
        }
        //对该结点的左子树进行查找
        TreeNode leftNode = find(root.left,val);
        if (leftNode != null) {
            return leftNode;
        }
        //对该节点的右子树进行查找
        TreeNode rightNode = find(root.right,val);
        return rightNode;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        //空树默认为完全二叉树
        if (root == null) {
            return true;
        }
        //进行层序遍历
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();

            if (cur == null) {
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        //循环结束后判断队列中是否存在有效结点，存在则返回false
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                return false;
            }
        }
        return true;
    }
}
